package com.xposed.aliproxy.socket;

import android.content.Context;

import com.xposed.aliproxy.R;
import com.xposed.aliproxy.qrcode.GlobalInstance;
import com.xposed.aliproxy.utils.ParamConfig;
import com.xposed.aliproxy.utils.Utils;
import com.xposed.aliproxy.utils.XposeUtil;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import de.robv.android.xposed.XposedBridge;

public class SocketManager {
    /* renamed from: a */
    public static DataInputStream f1776a;
    /* renamed from: b */
    public static DataOutputStream f1777b;
    /* renamed from: c */
    private static Socket f1778c;
    /* renamed from: d */
    private static Context f1779d;
    /* renamed from: e */
    private static ParamConfig f1780e;
    /* renamed from: f */
    private static SocketManager f1781f;

    /* compiled from: SocketManager */
    /* renamed from: g.a$1 */
    static class C05211 implements Runnable {
        C05211() {
        }

        public void run() {
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("transType", "P011");
                jSONObject.put("inAccount", SocketManager.f1780e.getString("aliAccount", ""));
                jSONObject.put("sign", Utils.formatJson(jSONObject, SocketManager.f1779d.getString(R.string.app_xposed_jka)));
                String b = XposeUtil.m3000b(jSONObject.toString());
                SocketManager.f1781f.mo3617a(b);
                XposeUtil.xposedLog("socketWrite->", b);
                if (SocketManager.f1776a != null) {
                    SocketManager.f1776a.close();
                }
                if (SocketManager.f1777b != null) {
                    SocketManager.f1777b.close();
                }
                SocketManager.f1778c.close();
                XposeUtil.xposedLog("socket", "socket is closed");
            } catch (Throwable th) {
                SocketManager.f1778c = null;
                SocketManager.f1781f = null;
            }
            SocketManager.f1778c = null;
            SocketManager.f1781f = null;
        }
    }

    public SocketManager(Context context) throws Exception {
        f1779d = context;
        f1780e = ParamConfig.getInstance(context);
        f1778c = new Socket(f1780e.getString("host", null), Integer.parseInt(f1780e.getString("port2", null)));
        f1778c.setKeepAlive(true);
        f1777b = new DataOutputStream(f1778c.getOutputStream());
        f1776a = new DataInputStream(f1778c.getInputStream());
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("transType", "P009");
        jSONObject.put("termType", "2");
        jSONObject.put("inAccount", f1780e.getString("aliAccount", ""));
        jSONObject.put("sign", Utils.formatJson(jSONObject, context.getString(R.string.app_xposed_jka)));
        GlobalInstance.getInstance().stringArrayList.add(jSONObject.toString());
    }

    /* renamed from: a */
    public static SocketManager m2947a(Context context) {
        try {
            if (f1778c == null || f1778c.isClosed() || !f1778c.isConnected() || f1778c.isInputShutdown() || f1778c.isOutputShutdown()) {
                f1781f = null;
            }
            if (f1781f == null) {
                f1781f = new SocketManager(context);
            }
        } catch (Throwable th) {
            XposedBridge.log(th);
            f1781f = null;
        }
        return f1781f;
    }

    /* renamed from: a */
    public String mo3616a() throws Exception {
        if (f1778c == null) {
            return null;
        }
        byte[] bArr = new byte[4];
        while (f1776a.available() < 4) {
            Thread.sleep(100);
            if (GlobalInstance.getInstance().f1764c) {
                return null;
            }
        }
        f1776a.read(bArr, 0, 4);
        int parseInt = Integer.parseInt(new String(bArr));
        byte[] bArr2 = new byte[parseInt];
        while (f1776a.available() < parseInt) {
            Thread.sleep(100);
            if (GlobalInstance.getInstance().f1764c) {
                return null;
            }
        }
        f1776a.read(bArr2, 0, parseInt);
        String str = new String(bArr2, HTTP.UTF_8);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(parseInt);
        stringBuilder.append("-");
        stringBuilder.append(str);
        XposeUtil.xposedLog("socketRead->", stringBuilder.toString());
        return str;
    }

    /* renamed from: a */
    public void mo3617a(String str) throws IOException {
        if (f1778c != null) {
            byte[] bytes = str.getBytes(HTTP.UTF_8);
            XposeUtil.xposedLog("socketWrite->", str);
            f1777b.write(bytes);
            f1777b.flush();
        }
    }

    /* renamed from: b */
    public static void m2950b() {
        if (f1781f != null && f1778c != null) {
            new Thread(new C05211()).start();
        }
    }
}
